﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Scap.Info;
using Scap.Data;

namespace Scap.Web.Controllers
{
    public class EmployeeController : Controller
    {
        private ScapData db = new ScapData();

        //
        // GET: /Employee/DeleteEmployee
        public ActionResult DeleteEmployee(int id)
        {
            ScapEmployee scapemployee = db.Employees.Find(id);

            if (scapemployee != null)
            {
                db.Employees.Remove(scapemployee);
                db.SaveChanges();
                
            }            
            return RedirectToAction("Index");
        }

        //
        // GET: /Employee/

        public ActionResult Index()
        {
            //var employees = db.Employees.Include(s => s.OriginCity).Include(s => s.ResidenceCity);
            //return View(employees.ToList());

            if (TempData["MessageError"] != null)
            {
                ViewBag.MessageError = TempData["MessageError"]; 
            }

            return View();
        }

        //
        // GET: /Employee/Details/5

        public ActionResult Details(int id = 0)
        {
            ScapEmployee scapemployee = db.Employees.Find(id);
            if (scapemployee == null)
            {
                return HttpNotFound();
            }
            return View(scapemployee);
        }

        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            ViewBag.OriginCityId = new SelectList(db.LocationCities, "Id", "Name");
            ViewBag.ResidenceCityId = new SelectList(db.LocationCities, "Id", "Name");
            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(ScapEmployee scapemployee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(scapemployee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.OriginCityId = new SelectList(db.LocationCities, "Id", "Name", scapemployee.OriginCityId);
            ViewBag.ResidenceCityId = new SelectList(db.LocationCities, "Id", "Name", scapemployee.ResidenceCityId);
            return View(scapemployee);
        }

        //
        // GET: /Employee/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ScapEmployee scapemployee = db.Employees.Find(id);
            if (scapemployee == null)
            {
                return HttpNotFound();
            }
            ViewBag.OriginCityId = new SelectList(db.LocationCities, "Id", "Name", scapemployee.OriginCityId);
            ViewBag.ResidenceCityId = new SelectList(db.LocationCities, "Id", "Name", scapemployee.ResidenceCityId);
            return View(scapemployee);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(ScapEmployee scapemployee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(scapemployee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.OriginCityId = new SelectList(db.LocationCities, "Id", "Name", scapemployee.OriginCityId);
            ViewBag.ResidenceCityId = new SelectList(db.LocationCities, "Id", "Name", scapemployee.ResidenceCityId);
            return View(scapemployee);
        }

        //
        // GET: /Employee/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ScapEmployee scapemployee = db.Employees.Find(id);
            if (scapemployee == null)
            {
                return HttpNotFound();
            }
            return View(scapemployee);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ScapEmployee scapemployee = db.Employees.Find(id);
            db.Employees.Remove(scapemployee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}